package com.cloudree.gllib.program;

import android.content.Context;
import android.opengl.GLES20;
import android.support.annotation.RawRes;

/**
 * Created by Administrator on 2017/10/17 0017.
 */
public class CubeProgram extends ShaderProgram {

    private int mMVPMatrixHandle;
    private int mTextureUnitHandle;
    private int mTextureHandle;

    private int mPositionHandle;

    private int mColorHandle;
    public static final String[] attr = {A_POSITION, A_TEXTURE};
    public CubeProgram(Context context, @RawRes int vertexId, @RawRes int fragmentId) {
        super(context, vertexId, fragmentId, attr);

        bindShaderLocation();
    }

    public int getPositionHandle() {
        return mPositionHandle;
    }

    public int getMVPMatrixHandle() {
        return mMVPMatrixHandle;
    }

    public int getColorHandle() {
        return mColorHandle;
    }

    public int getTextureHandle() {
        return mTextureHandle;
    }

    public int getTextureUnitHandle() {
        return mTextureUnitHandle;
    }

    /**
     * bind shader location
     */
    private void bindShaderLocation() {
        mMVPMatrixHandle = GLES20.glGetUniformLocation(program, U_MVP_MATRIX);
        mPositionHandle = GLES20.glGetAttribLocation(program, A_POSITION);
        mColorHandle = GLES20.glGetUniformLocation(program, U_COLOR);

        mTextureUnitHandle = GLES20.glGetUniformLocation(program, U_TEXTURE_UNIT);
        mTextureHandle = GLES20.glGetAttribLocation(program, A_TEXTURE);
    }

    /**
     * set uniforms
     */
    public void setUniforms(float[] matrix, float r, float g, float b, int textureId) {
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, matrix, 0);
        GLES20.glUniform4f(mColorHandle, r, g, b, 1.0f);

        /**
         * set texture
         */
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
        GLES20.glUniform1i(mTextureUnitHandle, 0);
    }


}
